import nProgress from 'nprogress';
import type {RouteRecordRaw} from 'vue-router';

import 'nprogress/nprogress.css';

import { defaultSetting } from '@/settings';
import { useRouteStore, useUserStore } from '@/store';
import { isExternal } from '@/utils';
import { getToken } from '@/utils/auth';

import router from './routers';

nProgress.configure({ showSpinner: false });

router.beforeEach((to, from, next) => {
  nProgress.start();
  if (to.meta.title) {
    document.title = to.meta.title as string + ' | ' + defaultSetting.title;
  }

  // 检查是否登录
  if (getToken()) {
    // 如果已登录，访问登录页面，直接跳转到首页
    if (to.path === '/login') {
      next({ path: '/' });
    } else {
      // 为了避免重复设置路由
      if (!useUserStore().roles?.length) {
        useUserStore()
          .getInfo()
          .then(() => {
            useRouteStore()
              .generateRoutes()
              .then((res) => {
                res.forEach((route) => {
                  if (!isExternal(route.path)) {
                    router.addRoute(route as RouteRecordRaw);
                  }
                });
                next({ ...to, replace: true });
              })
              .catch((err) => {
                console.error(err);
                next();
              });
          })
          .catch((err) => {
            // logout
            console.error(err);
            next();
          });
      } else {
        next();
      }
    }
  } else {
    // 未登录，检查访问的路径是否在白名单中，如果是，直接访问，否则跳转到登录页面
    if (defaultSetting.anonymousAccessWhiteList.includes(to.path)) {
      next();
    } else {
      next(`/login?redirect=${to.fullPath}`);
    }
  }
});

router.afterEach(() => {
  nProgress.done();
});
